<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" type="text/css" href="/layui/css/layui.css" media="all">
    <style>
        html, body {
            width: 100%;
            height: 100%
        }

        body {
            background: url("https://bing.ioliu.cn/photo/SunbeamsForest_ZH-CN5358008117?force=download") no-repeat;
            background-size: 100%;
        }
    </style>
</head>
<body>
<div style="margin-top: 10%">
    <div class="layui-fluid">
        <div class="layui-row">
            <div class="layui-col-sm4 layui-col-md-offset4">
                <div class="layui-card">
                    <div class="layui-card-header"><strong>Spring Security入门到实践自定义登录</strong></div>
                    <div class="layui-card-body">
                        <form id="security-form" action="/login" method="post" class="layui-form"
                              lay-filter="spring-security-example">
                            <div class="layui-form-item">
                                <label class="layui-form-label">用户名</label>
                                <div class="layui-input-block">
                                    <label>
                                        <input type="text" name="username" lay-verify="username" autocomplete="off"
                                               placeholder="请输入用户名"
                                               class="layui-input">
                                    </label>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">密码</label>
                                <div class="layui-input-block">
                                    <label>
                                        <input type="password" name="password" lay-verify="password" placeholder="请输入密码"
                                               autocomplete="off"
                                               class="layui-input">
                                    </label>
                                </div>
                            </div>

                            <div class="layui-form-item" style="margin-left: 20%">
                                <div class="layui-input-block">
                                    <button type="submit" class="layui-btn" lay-submit="" lay-filter="submit">立即登录
                                    </button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="/layui/layui.js" type="text/javascript" charset="utf-8"></script>
<script>
    layui.use(['form', 'layer', 'jquery'], function () {
        var form = layui.form
            , layer = layui.layer
            , $ = layer.jquery;
        var $form = $('#security-form');

        //自定义验证规则
        form.verify({
            username: function (value) {
                if (value.length === 0) {
                    return '用户名不能为空';
                }
            }
            , password: [
                /^[\S]{6,18}$/
                , '密码必须6到18位，且不能出现空格'
            ]
        });

        //监听提交
        form.on('submit(submit)', function (data) {
            $.ajax({
                url: $form.attr('action'),
                data: data.field,
                type: $form.attr('method'),
                dataType: 'json',
                error: function () {
                    layer.msg('提交失败，您的网络有问题！', {icon: 5, anim: 6});
                }
            });
            // 防止表单自动提交
            return false;
        });
    });
</script>
</body>
</html>